//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * systemUserAction
 * 项目名称:代购系统
 * @author Mengxiangliang
 * 功能描述:
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * @version [V1] 
 */
package com.gamutsoft.amb.user.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.gamutsoft.amb.base.action.BaseAction;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.common.util.CookieUtil;
import com.gamutsoft.amb.user.model.SystemUser;
import com.gamutsoft.amb.user.service.ISystemUserService;


@SuppressWarnings({"serial","rawtypes"})
public class SystemUserAction extends BaseAction {
	
	private SystemUser systemUser;
	
	private List<Map> systemUserList;
	
	
	private ISystemUserService systemUserService;
	
	private String rememberLogin;
	
	private String errorCode;
    
	/**
	 * 跳转到用户登录页面
	 */
	public String gotoLogin()throws BusinessException, Exception {
		return "do_login";
	}
	/**
	 * 执行登录页面
	 */
	public String doLogin()throws BusinessException, Exception {
		if(systemUser!=null){
			String userName=systemUser.getUsername();
			String password=systemUser.getPassword();
			SystemUser bean =new SystemUser();
			if(userName!=null && !"".equals(userName)){
				//判断是邮箱还是用户名
				if(userName.indexOf("@")!=-1){//是邮箱
					bean.setEmail(userName.trim());
				}else{//是用户名
					bean.setUsername(userName.trim());
				}
			}else{
				errorCode="01";
			}
			if(password!=null && !"".equals(password)){
				Md5PasswordEncoder  mp = new Md5PasswordEncoder();
				password=mp.encodePassword(password.trim(), Constants.DAIGOU_KEY);
				bean.setPassword(password);
			}else{
				errorCode="01";
			}
			if(errorCode==null || "".equals(errorCode)){
				SystemUser loginUser=systemUserService.loginByUser(bean);
				if(loginUser!=null){//用户存在
					//保持登录状态
					if(rememberLogin!=null && !"".equals(rememberLogin) && "01".equals(rememberLogin)){
						//保存cookie
						CookieUtil.addCookiesUser(this.getResponse(), loginUser.getEmail(), loginUser.getPassword());
					}
					//保存session
					this.getRequest().getSession().setAttribute(Constants.SESSION_USER, loginUser);
					return "index";
				}else{
					errorCode="01";
				}
			}
			
		}
		return "do_login";
		
	}
	/**
	 * 跳转到注册页面
	 */
	public String gotoReg()throws BusinessException, Exception {
		return "do_reg";
	}
    /**
     * 注册条款页面
     */
	public String gotoItems()throws BusinessException, Exception {
		return "reg_item";
	}
	public String doReg()throws BusinessException, Exception {
		try{
			if(systemUser!=null){
				SystemUser user=systemUserService.createSystemUser(systemUser);
				//保存session
				this.getRequest().getSession().setAttribute(Constants.SESSION_USER, user);
				//发送邮件
			//	SendMailUtil.sendMail("title", "content", "email");
			}
		}catch(Exception e){
			log.error(e,e);
		}
		return "index";
	}
	/**
	 * ajax验证邮箱唯一
	 */
	public void ajaxCheckEmail()throws BusinessException, Exception {
		PrintWriter out;
		out = this.getResponse().getWriter();
		String result="01";//表示该邮箱可用
		try{
			String email=this.getRequest().getParameter("email");
			if(email!=null && !"".equals(email)){
				SystemUser bean =new SystemUser();
				bean.setEmail(email);
				List<Map> list=systemUserService.searchSystemUserListByWhere(new QueryExample(), bean);
				if(list!=null && list.size()!=0){//该邮箱已经被注册
					result="02";//邮箱已被注册
				}
			}
		}catch(Exception e){
			log.error(e,e);
		}
		out.write(result);
	}
	/**
	 * ajax验证用户名唯一
	 */
	public void ajaxCheckUserName()throws BusinessException, Exception {
		PrintWriter out;
		out = this.getResponse().getWriter();
		String result="01";//表示该用户名可用
		try{
			String userName=this.getRequest().getParameter("userName");
			if(userName!=null && !"".equals(userName)){
				SystemUser bean =new SystemUser();
				bean.setUsername(userName);
				List<Map> list=systemUserService.searchSystemUserListByWhere(new QueryExample(), bean);
				if(list!=null && list.size()!=0){//该用户名已经被注册
					result="02";//用户名已被注册
				}
			}
		}catch(Exception e){
			log.error(e,e);
		}
		out.write(result);
	}
	/**
	 * 快速登录页面
	 */
	public String gotoQuickLogin()throws BusinessException, Exception {
		return "quick_login";
	}
	/**
	 * 快速登录
	 */
	public void doQuickLogin()throws BusinessException, Exception {
		PrintWriter out;
		out = this.getResponse().getWriter();
		try{
			String userName=systemUser.getUsername();
			String password=systemUser.getPassword();
			SystemUser bean =new SystemUser();
			if(userName!=null && !"".equals(userName)){
				//判断是邮箱还是用户名
				if(userName.indexOf("@")!=-1){//是邮箱
					bean.setEmail(userName);
				}else{//是用户名
					bean.setUsername(userName);
				}
			}else{
				errorCode="01";
			}
			if(password!=null && !"".equals(password)){
				Md5PasswordEncoder  mp = new Md5PasswordEncoder();
				password=mp.encodePassword(password, Constants.DAIGOU_KEY);
				bean.setPassword(password);
			}else{
				errorCode="01";
			}
			if(errorCode==null || "".equals(errorCode)){
				SystemUser loginUser=systemUserService.loginByUser(bean);
				if(loginUser!=null){//用户存在
					//保持登录状态
					if(rememberLogin!=null && !"".equals(rememberLogin) && "01".equals(rememberLogin)){
						//保存cookie
						CookieUtil.addCookiesUser(this.getResponse(), loginUser.getEmail(), loginUser.getPassword());
					}
					//保存session
					this.getRequest().getSession().setAttribute(Constants.SESSION_USER, loginUser);
				}
			}
			if(errorCode==null || "".equals(errorCode)){
				out.write("01");//表示登录成功
			}else{
				out.write("02");//登录失败
			}
		}catch(Exception e){
			log.error(e,e);
		}
	}
	 /**
     * ajax验证用户是否已经登录
     */
	public void ajaxUserHasLogin()throws BusinessException, Exception{
		PrintWriter out;
		out = this.getResponse().getWriter();
		try{
		  Object obj=this.getRequest().getSession().getAttribute(Constants.SESSION_USER);
		  if(obj!=null){
			  out.write("01");//已有用户登录
		  }else{
			  out.write("02");//没有有用户登录
		  }
		 
		}catch(Exception e){
			log.error(e,e);
		}
	}
	/**
	 * @return the systemUser
	 */
	public SystemUser getSystemUser() {
		return systemUser;
	}

	/**
	 * @param systemUser the systemUser to set
	 */
	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	/**
	 * @return the systemUserList
	 */
	public List<Map> getSystemUserList() {
		return systemUserList;
	}

	/**
	 * @param systemUserList the systemUserList to set
	 */
	public void setSystemUserList(List<Map> systemUserList) {
		this.systemUserList = systemUserList;
	}


	/**
	 * @param systemUserService the systemUserService to set
	 */
	public void setSystemUserService(ISystemUserService systemUserService) {
		this.systemUserService = systemUserService;
	}
	public String getRememberLogin() {
		return rememberLogin;
	}
	public void setRememberLogin(String rememberLogin) {
		this.rememberLogin = rememberLogin;
	}
	public String getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}
	
}
